package com.example.seckill.interfaces;

import com.example.seckill.SeckillApplication;
import com.example.seckill.application.impl.SeckillService;
import com.example.seckill.application.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Martin
 */
@Slf4j
@RestController
@RequestMapping("/seckill")
public class SeckillFacade {

    @Autowired
    private SeckillService seckillService;

    /**
     * 秒杀下单接口
     *
     * @param productId 商品ID
     * @param userId  购买数量
     * @return 下单结果
     */
    @PostMapping("/order")
    public ResponseEntity<String> placeOrder(@RequestParam("productId") Long productId,
                                             @RequestParam("userId") Long userId) {
        boolean success = seckillService.seckill(productId, userId);
        if (success) {
            return ResponseEntity.ok("下单成功");
        } else {
            return ResponseEntity.badRequest().body("下单失败");
        }
    }
}

